I am trying to add some post titles from a custom post category. I currently have it printing the correct amount of <li> but unfortunately it is the same name. You will see I narrow it down using meta, so I am getting 5 results all the same name.
I do not pretend to be a pro at this, so I am humbly asking for any help that the community may have.
Thanks In advance.
I have tried doing a foreach and also doing the if have_posts. both have yielded the same result.
$page_title = get_the_title();
$args = array(
'orderby' => 'title',
'post_type' => 'person',
'post_status' => 'publish',
'meta_key' => 'division',
'meta_value' => 'Singles'
);
$string = '';
$query = new WP_Query( $args );
if( $query->have_posts() ):
$string .= '<ul>';
while( $query->have_posts() ):
$query->the_post();
$string .= '<li>' . get_the_title() . '</li>';
endwhile;
$string .= '</ul>';
echo $string;
wp_reset_postdata();
else :
// No, we don't have any posts, so maybe we display a nice message
echo "<p class='no-posts'>" . __( "Sorry, there are no posts at this time." ) . "</p>";
endif;
So what we are going for is looking for how many posts exist in "Singles" (in my case that is 5) and printing the post title for each one in an <li>. Currently it prints 1 of the 5, 5 times.
get_the_title() has the following implementation
function get_the_title( $post = 0 ) {
$post = get_post( $post );
$title = isset( $post->post_title ) ? $post->post_title : '';
$id = isset( $post->ID ) ? $post->ID : 0;
if ( ! is_admin() ) {
if ( ! empty( $post->post_password ) ) {
/**
* Filters the text prepended to the post title for protected posts.
*
* The filter is only applied on the front end.
*
* #since 2.8.0
*
* #param string $prepend Text displayed before the post title.
* Default 'Protected: %s'.
* #param WP_Post $post Current post object.
*/
$protected_title_format = apply_filters( 'protected_title_format', __( 'Protected: %s' ), $post );
$title = sprintf( $protected_title_format, $title );
} elseif ( isset( $post->post_status ) && 'private' == $post->post_status ) {
/**
* Filters the text prepended to the post title of private posts.
*
* The filter is only applied on the front end.
*
* #since 2.8.0
*
* #param string $prepend Text displayed before the post title.
* Default 'Private: %s'.
* #param WP_Post $post Current post object.
*/
$private_title_format = apply_filters( 'private_title_format', __( 'Private: %s' ), $post );
$title = sprintf( $private_title_format, $title );
}
}
/**
* Filters the post title.
*
* #since 0.71
*
* #param string $title The post title.
* #param int $id The post ID.
*/
return apply_filters( 'the_title', $title, $id );
}
As you can see you can pass a post to it and then it will get its title. Since you didn't pass a post to it, it didn't get a title for you. Suggestion:
$page_title = get_the_title();
$args = array(
'orderby' => 'title',
'post_type' => 'person',
'post_status' => 'publish',
'meta_key' => 'division',
'meta_value' => 'Singles'
);
$string = '';
$query = new WP_Query( $args );
if( $query->have_posts() ):
$string .= '<ul>';
while( $query->have_posts() ):
$string .= '<li>' . get_the_title($query->the_post()) . '</li>';
endwhile;
$string .= '</ul>';
echo $string;
wp_reset_postdata();
else :
// No, we don't have any posts, so maybe we display a nice message
echo "<p class='no-posts'>" . __( "Sorry, there are no posts at this time." ) . "</p>";
endif;
Related
In a WP site I'm trying to set up an events page. I've set up a custom post type and defined a metabox using the metabox.io plugin. The metabox contains a date-picker with the id of 'date_1'. I am now attempting to find a way to display a list of posts with the date and title of the event. Title works just fine but I'm having trouble getting the date to display.
The line
$events .= '' . get_post_meta(get_the_ID(), 'date_1', true) . " - " . get_the_title() .'';
returns title, but not the date. If I wrap the get_post_meta line in a print_r it returns a 1.
I've also experimented with:
$events .= '' . get_post_meta($post->ID, $field['date_1'], TRUE ). " - " . get_the_title() .'';
but this returns "array" instead of one.
Full code
if ( ! function_exists('events_shortcode') ) {
function events_shortcode() {
$args = array(
'post_type' => 'kalender',
'post_status' => 'publish',
'order' => 'ASC',
'posts_per_page' => 10,
);
$postslist = new WP_Query( $args );
global $post;
if ( $postslist->have_posts() ) :
$events .= '<div class="events-lists">';
while ( $postslist->have_posts() ) : $postslist->the_post();
$events .= '<div class="items">';
$events .= '' . get_post_meta(get_the_ID(), 'date_1', true) . " - " . get_the_title() .'';
$events .= '</div>';
endwhile;
wp_reset_postdata();
$events .= '</div>';
endif;
return $events;
}
add_shortcode( 'events', 'events_shortcode' );
}
Please read metabox.io plugin date field documentation.
I hope you can fix your problem
Is there any way to get only parent terms from custom taxonomy or category?
$genres = get_the_term_list($post->ID, 'genres', '<div class="genres"><div class="mta">', '', '</div></div>');
You can build the list alone without childrens.
/**
* #param post_id can use 'get_the_ID()'
* #param taxonomy for example 'category'
*/
$terms = wp_get_object_terms($post_id, $taxonomy, array('parent'=>'0'));
foreach($terms as $term) {
?>
<?php echo $term->name; ?>
<?php
}
The second option is that you can filter the terms and remove all the terms with parent bigger than 0 for the get_the_term_list() function.
function remove_child_terms($terms, $post_id, $taxonomy) {
/**
* Add some condition here to limit this for your custom taxonomy
* if($taxonomy == 'something') {
*/
foreach($terms as $key => $term) {
if($term->parent !== 0) {
unset($terms[$key]);
}
}
/**
* }
*/
return $terms;
}
add_filter( 'get_the_terms', 'remove_child_terms', 10, 3 );
Try this code,
<?php $terms = get_terms( array(
'taxonomy' => 'taxonomy_name',
'parent' => 0
) );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
} ?>
I need to add a wrapper div around all the content within the author box.
What filter code do I need to change the genesis_author_box into this:
<section class="author-box" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
<div class="author-box-wrap">
<img alt="" src="" srcset="" class="avatar avatar-150 photo" height="150" width="150">
<h4 class="author-box-title">About <span itemprop="name">Author Name</span></h4>
<div class="author-box-content" itemprop="description">Description Text</div>
</div>
</section>
Here is the default Genesis markup:
<section class="author-box" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
<img alt="" src="" srcset="" class="avatar avatar-150 photo" height="150" width="150">
<h4 class="author-box-title">About <span itemprop="name">Author Name</span></h4>
<div class="author-box-content" itemprop="description">Description Text</div>
</section>
Here is the code that assembles the default Genesis markup:
/**
* Echo the the author box and its contents.
*
* The title is filterable via `genesis_author_box_title`, and the gravatar size is filterable via
* `genesis_author_box_gravatar_size`.
*
* The final output is filterable via `genesis_author_box`, which passes many variables through.
*
* #since 1.3.0
*
* #global WP_User $authordata Author (user) object.
*
* #param string $context Optional. Allows different author box markup for different contexts, specifically 'single'.
* Default is empty string.
* #param bool $echo Optional. If true, the author box will echo. If false, it will be returned.
* #return string HTML for author box if `$echo` param is falsy.
*/
function genesis_author_box( $context = '', $echo = true ) {
global $authordata;
$authordata = is_object( $authordata ) ? $authordata : get_userdata( get_query_var( 'author' ) );
$gravatar_size = apply_filters( 'genesis_author_box_gravatar_size', 70, $context );
$gravatar = get_avatar( get_the_author_meta( 'email' ), $gravatar_size );
$description = wpautop( get_the_author_meta( 'description' ) );
// The author box markup, contextual.
if ( genesis_html5() ) {
$title = __( 'About', 'genesis' ) . ' <span itemprop="name">' . get_the_author() . '</span>';
/**
* Author box title filter.
*
* Allows you to filter the title of the author box. $context passed as second parameter to allow for contextual filtering.
*
* #since unknown
*
* #param string $title Assembled Title.
* #param string $context Context.
*/
$title = apply_filters( 'genesis_author_box_title', $title, $context );
$heading_element = 'h1';
if ( 'single' === $context && ! genesis_get_seo_option( 'semantic_headings' ) ) {
$heading_element = 'h4';
} elseif ( genesis_a11y( 'headings' ) || get_the_author_meta( 'headline', (int) get_query_var( 'author' ) ) ) {
$heading_element = 'h4';
}
$pattern = sprintf( '<section %s>', genesis_attr( 'author-box' ) );
$pattern .= '%s<' . $heading_element . ' class="author-box-title">%s</' . $heading_element . '>';
$pattern .= '<div class="author-box-content" itemprop="description">%s</div>';
$pattern .= '</section>';
} else {
$title = apply_filters( 'genesis_author_box_title', sprintf( '<strong>%s %s</strong>', __( 'About', 'genesis' ), get_the_author() ), $context );
$pattern = '<div class="author-box">%s<h1>%s</h1><div>%s</div></div>';
if ( 'single' === $context || get_the_author_meta( 'headline', (int) get_query_var( 'author' ) ) ) {
$pattern = '<div class="author-box"><div>%s %s<br />%s</div></div>';
}
}
$output = sprintf( $pattern, $gravatar, $title, $description );
/**
* Author box output filter.
*
* Allows you to filter the full output of the author box.
*
* #since unknown
*
* #param string $output Assembled output.
* #param string $context Context.
* #param string $pattern (s)printf pattern.
* #param string $context Gravatar.
* #param string $context Title.
* #param string $context Description.
*/
$output = apply_filters( 'genesis_author_box', $output, $context, $pattern, $gravatar, $title, $description );
if ( $echo ) {
echo $output;
return null;
} else {
return $output;
}
My PHP is really rough... Thank you for your help!
Give this a try:
add_filter( 'genesis_author_box', 'my_filter_author_box', 10, 3 );
function my_filter_author_box( $output, $context ) {
$output = preg_replace( '/<section (.+?)>(.+)<\/section>/ms', '<section $1><div class="author-box-wrap">$2</div></section>', $output );
return $output;
}
It may be worth reconsidering why you need to add the inner wrapping div and see if you can work out a better way to style it instead.
I have a custom post type that has a bunch of posts all formatted like so
Artist - Song Title
for example
The Smashing Pumpkins - Quiet
I am trying to put 'Artist' in a variable $artist and 'Song Title' in a variable $song
$artistsong = get_the_title();
$songeach = explode("-", $artistsong);
$artist = $songeach[0];
$song = $songeach[1];
But this does not work. Echo-ing $artist gets the full title
The Smashing Pumpkins - Quiet
and echoing $song does not output anything
This works if I am just starting from plaintext, but not with 'get_the_title()'
$song = "The Smashing Pumpkins - Quiet";
$songeach = explode("-", $song);
$artist = trim($songeach[0]);
$song = trim($songeach[1]);
echo $artist;
//echos 'The Smashing Pumpkins'
echo $song;
//echos 'Quiet'
Is there another way to put the full title into a variable initially other than get_the_title() which does not seem to be working for me, or am I missing something else?
Add this code to your functions.php
function get_the_title_keep_hyphen( $post = 0 ) {
$post = get_post( $post );
$title = isset( $post->post_title ) ? $post->post_title : '';
$id = isset( $post->ID ) ? $post->ID : 0;
if ( ! is_admin() ) {
if ( ! empty( $post->post_password ) ) {
/**
* Filter the text prepended to the post title for protected posts.
*
* The filter is only applied on the front end.
*
* #since 2.8.0
*
* #param string $prepend Text displayed before the post title.
* Default 'Protected: %s'.
* #param WP_Post $post Current post object.
*/
$protected_title_format = apply_filters( 'protected_title_format', __( 'Protected: %s' ), $post );
$title = sprintf( $protected_title_format, $title );
} elseif ( isset( $post->post_status ) && 'private' == $post->post_status ) {
/**
* Filter the text prepended to the post title of private posts.
*
* The filter is only applied on the front end.
*
* #since 2.8.0
*
* #param string $prepend Text displayed before the post title.
* Default 'Private: %s'.
* #param WP_Post $post Current post object.
*/
$private_title_format = apply_filters( 'private_title_format', __( 'Private: %s' ), $post );
$title = sprintf( $private_title_format, $title );
}
}
/**
* Filter the post title.
*
* #since 0.71
*
* #param string $title The post title.
* #param int $id The post ID.
*/
return $title;
}
And use this code in your single.php
$artistsong = get_the_title_keep_hyphen();
$songeach = explode(" - ", $artistsong);
$artist = $songeach[0];
$song = $songeach[1];
See the last line
I change from return apply_filters( 'the_title', $title, $id ); to return $title;
Because apply_filters function change the hyphen from - => –.
It's because of the dash symbol.
Try $songeach = explode("P", $artistsong); and you'll see what I mean. You could try a different character between artist and song title - although probably not ideal.
Pagination Doesn't work with word press custom post.
i have tried these code below,but pagination doesn't appear.
tried different query but no result.
i am new in php and word press.i just copy and past code.
can anyone please help me?
what i have done so far are below.
in function.php
/*pagination*/
function wpbeginner_numeric_posts_nav() {
if( is_singular() )
return;
global $wp_query;
/** Stop execution if there's only 1 page */
if( $wp_query->max_num_pages <= 1 )
return;
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = intval( $wp_query->max_num_pages );
/** Add current page to the array */
if ( $paged >= 1 )
$links[] = $paged;
/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
if ( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo '<div class="navigation"><ul>' . "\n";
/** Previous Post Link */
if ( get_previous_posts_link() )
printf( '<li>%s</li>' . "\n", get_previous_posts_link() );
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
if ( ! in_array( 2, $links ) )
echo '<li>…</li>';
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}
/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li>…</li>' . "\n";
$class = $paged == $max ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}
/** Next Post Link */
if ( get_next_posts_link() )
printf( '<li>%s</li>' . "\n", get_next_posts_link() );
echo '</ul></div>' . "\n";
}
in custom post query
<?php
global $post;
$args = array( 'posts_per_page' => 5, 'post_type'=> 'latestnews');
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);
?>
<div class="page_news">
<div class="single_page_news">
<h2><?php the_title(); ?><h2>
<p><?php the_content(); ?></p>
</div>
</div>
<?php endforeach; ?>
<?php wpbeginner_numeric_posts_nav(); ?>
please help me
Use These code
$paged = ( get_query_var('page') ) ? get_query_var('page') :1;
$query = new WP_Query( array( 'posts_per_page' => 1,'paged' => $paged,'post_type' => 'achievements','orderby' => 'date', 'order' => 'ASC' ) );
while ( $query->have_posts() ) : $query->the_post();
endwhile;
Instead of
$args = array( 'posts_per_page' => 5, 'post_type'=> 'latestnews');
$myposts = get_posts( $args );
Follow this link
http://thenetapp.com/2014/01/how-to-list-wordpress-posts-with-pagination/
Your pagination function is only set up for the default main query, not for a custom query.
Also, don't use get_posts for paginated queries. It it is nice function to use for a custom query, but it becomes a bummer to work with once you require pagination.
Rather use WP_Query for paginated queries, it is much easier to use.
Example:
$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
$args = array(
'posts_per_page' => 1,
'paged' => $paged,
'post_type' => 'YOUR POST TYPE'
);
$q = new WP_Query($args);
if($q->have_post()) {
while($q->have_posts()) {
$q->the_post();
//YOUR LOOP
}
//YOUR PAGINATION
}
wp_reset_postdata();
You can have a look at the codex for extra parameters.
You now need to change every instance of $wp_query in your pagination function to $q for it to work.
Just a point of note, you don't have to call the $post global
EDIT
From your comments, there is a much easier way to accomplish your goal without any custom query
This page is an archive page that is meant to display your custom post type latestnews. You can simply just rename your archive-custom.php to archive-latestnews.php. See the Template Hierarchy. Just make sure has_archive is set to true when you register your post type
You should also never shop the main query for a custom query on any type of archive page. It is always troublesome, as you can see. So, delete your custom query and replace it with the default query
This is all you should have in your archive page
if(have_post()) {
while(have_posts()) {
the_post();
//YOUR LOOP
}
//YOUR PAGINATION
}
Just change all the instances of $q back to $wp_query again. Everything should work then
For extra info, you have to check out this post I've done on WPSE