Search

Table of Contents

Using Conditionals in Custom Author Layouts

You are here:

The PublishPress Authors plugin allows you to create custom author layouts. In this guide, we'll show you how to include conditionals in your custom layouts.


Conditionals for post data

This variable contains the current post in the query and can be used to create different layouts based on the post type, post status, or other post data.

You can choose to display the data or use it in conditionals checking post properties or custom fields (metadata).

{% if post.post_type == 'my-custom-post-type' %}

{% if post.post_type in ['post', 'page'] %}

{% if post.post_status == 'publish' %}

{% if post.post_status in ['publish', 'future'] %}

{% if post.post_name == 'the-post' %}

{% if post.ID == 2 %}

{{ post.post_title }}

Post's custom fields can be checked or displayed using the “get_meta” function:

{% if post.get_meta('the_meta_key') == 1 %}

{{ post.get_meta('another-meta-key') }}

The “get_meta” function accepts two arguments. The first one is the meta key, and the other is a boolean value that says the meta is single or not (default for true).

{% if 'the_meta_value' in post.get_meta('repeatable-metas', false) %}

{% for item in post.get_meta('repeatable-metas', false) %}

Main properties that are available:

  • ID
  • post_date
  • post_date_gmt
  • post_content
  • post_title
  • post_excerpt
  • post_status
  • post_name
  • post_password
  • post_modified
  • post_modified_gmt
  • post_parent
  • guid
  • post_type

Conditionals for the author box title

This part of the PublishPress Authors core and controls whether or not the title of the author box should be displayed. This boolean value can be controlled by the widget's settings, by the shortcode's parameters or action's parameters. If you kept the option “Show below the content” enabled, the value will be “true”.

You can use show_title in a conditional block using this code:

{% if show_title %}
    <h2 class="pp-multiple-authors-layout-{{ layout }} widget-title">{{ title }}</h2>
{% endif %}

Conditionals for the author email

This is boolean value that controls whether the author's email should be displayed. The value is set by the global option “Show email link” in the PublishPress Authors settings:

{% if show_email and author.user_email %}
    <a href="mailto:{{ author.user_email }}" target="_blank" rel="noopener noreferrer"><span class="dashicons dashicons-email"></span></a>
{% endif %}

Conditionals to target individual authors

It is possible to target code for some authors. For example, our customer wanted to hide the authors box for the “admin” user. Any code inside the conditional below will display for all authors except the “admin”.

{% if authors[0].slug != 'admin' %}
........
{% endif %}

Conditionals for the author's site URL

This is boolean value that controls if the author's site address/link should be displayed. The value is set by the global option “Show site link” in the PublishPress Authors settings:

{% if show_site and author.user_url %}
    <a href="{{ author.user_url }}" target="_blank" rel="noopener noreferrer"><span class="dashicons dashicons-admin-site"></span></a>
{% endif %}

Conditionals for the logged-in user

These variables allow you to conditionally show content in the author profile. These are based on who is viewing the author profile, not on the author themselves.

This conditional will display content only to logged-in users:

{% if current_user %}

This conditional will display content only if the user's ID is 32:

{% if current_user.id == 32 %}

This conditional will display content only if the user is in the Administrator role:

{% if 'administrator' in current_user.roles %}

This conditional will display content only if the user's display name is “the-user”:

{% if current_user.display_name == 'the-user' %}

This conditional will display content only if the logged-in user has a specific capability:

{% if current_user_can('the-capability-goes-here') %}

This conditional will display content only if the user has a specific metadata on the profile:

‚Äč{% if get_current_user_meta('the-custom-field') == 1 %}

Professional publishing plugins for WordPress! Get PublishPress