I've just started working on a project with wordpress. And I wonder what is the best practice to show a group of columns like this (highlighted in red)
in the page (post). I have a requirement that person who creating a post can insert default columns with different text and header.
I tried shortcode but it seems too complex. I want something like form with the input fields which will generate the colums. Thanks
I solve this problem with custom post types and a shortcode.
Plugins (optional - you could hand code everything)
Custom Post Types and Custom Fields creator - WCK
Intuitive Custom Post Order
Create your custom post type and fields.
Use Intuitive Custom Post Order to allow users to reorder the posts.
Customize this shortcode template to match your code and requirements:
add_shortcode('cpt-list', 'jpro_cpt_list');
function jpro_cpt_list() {
$args = array (
'post_type' => array( 'cpt' ),
'posts_per_page' => '-1',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$query = new WP_Query( $args );
$cpt_list = '<div class="jpro-cpt cpt-list container">';
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$post = get_post($id);
$postID = $post->ID;
$image = get_the_post_thumbnail( $postID, 'medium', array( 'class' => 'aligncenter' ));
$name = get_the_title($postID);
$bio = apply_filters('the_content', $post->post_content);
if(!empty($image)){
$output_image = '<div class="cpt-list image">'.$image.'</div>';
} else {
$output_image = '';
}
if(!empty($name)){
$output_name ='<h3 class="cpt-name cpt-list">'.$name.'</h3>';
} else {
$output_name = '';
}
if(!empty($bio)){
$output_bio ='<div class="cpt-bio cpt-list">'.$bio.'</div>';
} else {
$output_bio = '';
}
$cpt_list .= '<section class="jpro-cpt cpt-list columnClass">';
$cpt_list .= $output_image;
$cpt_list .= $output_name;
$cpt_list .= $output_bio;
$cpt_list .= '</section>';
}
} else {
$cpt_list .='<p>Ooops! No cpt Found.</p>';
}
$cpt_list .='</div>';
wp_reset_postdata();
return $cpt_list;
}
Related
I added the following code to my functions.php file in WordPress 6.1.1 to display excerpts.
function new_excerpt_length($length) {
return 100;
}
add_filter('excerpt_length', 'new_excerpt_length');
function new_excerpt_more($more) {
return '...';
}
add_filter('excerpt_more', 'new_excerpt_more');
...but I also have a use case to show the full excerpt without a read more link.
On page template 1 I add the below code to display the excerpt:
<?php echo the_excerpt(); ?>
...and it displays the excerpt as per the functions.php file but how do I create a 2nd excerpt without the read more link and apply it to page template 2?
Is there a parameter I can use within the_excerpt(parameter); or can I use something like wp_trim_excerpt https://developer.wordpress.org/reference/functions/wp_trim_excerpt/ maybe?
I came across the below code that is supposed to do what I want
function wpex_get_excerpt( $args = array() ) {
// Default arguments.
$defaults = array(
'post' => '',
'length' => 40,
'readmore' => false,
'readmore_text' => esc_html__( 'read more', 'text-domain' ),
'readmore_after' => '',
'custom_excerpts' => true,
'disable_more' => false,
);
// Apply filters to allow child themes mods.
$args = apply_filters( 'wpex_excerpt_defaults', $defaults );
// Parse arguments, takes the function arguments and combines them with the defaults.
$args = wp_parse_args( $args, $defaults );
// Apply filters to allow child themes mods.
$args = apply_filters( 'wpex_excerpt_args', $args );
// Extract arguments to make it easier to use below.
extract( $args );
// Get the current post.
$post = get_post( $post );
// Get the current post id.
$post_id = $post->ID;
// Check for custom excerpts.
if ( $custom_excerpts && has_excerpt( $post_id ) ) {
$output = $post->post_excerpt;
}
// No custom excerpt...so lets generate one.
else {
// Create the readmore link.
$readmore_link = '' . $readmore_text . $readmore_after . '';
// Check for more tag and return content if it exists.
if ( ! $disable_more && strpos( $post->post_content, '<!--more-->' ) ) {
$output = apply_filters( 'the_content', get_the_content( $readmore_text . $readmore_after ) );
}
// No more tag defined so generate excerpt using wp_trim_words.
else {
// Generate an excerpt from the post content.
$output = wp_trim_words( strip_shortcodes( $post->post_content ), $length );
// Add the readmore text to the excerpt if enabled.
if ( $readmore ) {
$output .= apply_filters( 'wpex_readmore_link', $readmore_link );
}
}
}
// Apply filters and return the excerpt.
return apply_filters( 'wpex_excerpt', $output );
}
Output using:
<?php echo wpex_get_excerpt ( $defaults = array(
'length' => 40,
'readmore' => true,
'readmore_text' => esc_html__( 'read more', 'wpex-boutique' ),
'custom_excerpts' => true,
) ); ?>
...but doesn't seem to workas intended. Outputs the excerpt with no link but the args don't see to work when changed. Would be perfect for my use otherwise. Maybe someone sees how to fix this code?
Thanks
I am trying to create a function for a shortcode to loop through an 'articles' custom post type. This is essentially a downloadable file uploaded by users and is a custom post type without the content field/editor box in the post editor. This custom post type also has a 'download_link' ACF field attached to it which is the location of the file for downloads. This is the href attribute for each post in the feed. However, the field is not generating any output in the function below. However the title is being populated. I know that all the article posts have a value provided for this particular field. Am I missing something? Thanks.
// CREATE A FEED OF ARTICLES WITH LINKS
function articles_download_feed(){
$articles_query = new WP_Query(array('post_type' => 'article',
'post_status' => 'any'));
if( $articles_query->have_posts() ){
$output = '<ul>';
while( $articles_query->have_posts() ){
$articles_query->the_post();
$articleID = get_the_ID();
$title = get_the_title($articleID);
if(get_field('download_link', $articleID)){
$download = get_field('download_link', $articleID, true);
} else {
$download = 'ABC';
}
$output .= '<li><a target="_blank" href="'.$download.'">'.$title.'</a></li>';
}
$output .= '</ul>';
wp_reset_postdata();
return $output;
}
} add_shortcode('articles_feed', 'articles_download_feed');
I managed to fix this issue myself.
I set up the 'dile_dl_link' custom field for 'article' type posts via the add_post_meta() WP function, instead of via Advanced Custom Fields (below is the segment of code to create a new article):
// CREATE NEW ARTICLE LINK FROM GRAVITY FORM SUBMISSION
function article_upload_createlink($entry, $form){
// Fields
$post_title = ucwords($entry[8]);
$post_status = 'publish';
$post_excerpt = $entry[9];
$post_type = 'article';
$dl_link = $entry[3];
// Post being created
$new_article = array(
'post_title' => $post_title,
'post_status' => $post_status,
'post_excerpt' => $post_excerpt,
'post_type' => $post_type,
);
// ID for post
$theId = wp_insert_post($new_article);
// Update download_link
add_post_meta($theId, 'file_dl_link', $dl_link);
} add_action( 'gform_after_submission_7', 'article_upload_createlink', 10, 2 );
The function in question as presented in the question has been changed as seen below:
// CREATE A FEED OF ARTICLES WITH LINKS
function articles_download_feed(){
$articles_query = new WP_Query(array('post_type' => 'article', 'post_status' => 'any'));
if( $articles_query->have_posts() ){
$output = '';
while( $articles_query->have_posts() ){
$articles_query->the_post();
$articleID = get_the_ID();
$title = get_the_title();
$excerpt = get_the_excerpt();
$get_dllink = get_post_meta($articleID, 'file_dl_link', true);
$output .= '<div class="wpb_wrapper">
<div class="vc_empty_space" style="height: 24px">
<span class="vc_empty_space_inner"></span>
</div>
<div class="wpb_text_column ">
<div class="wpb_wrapper">
<h4>'.$title.'</h4>
<p>'.$excerpt.'</p></div>
</div>
<div class="w-btn-wrapper align_left">
<a target="_blank" class="w-btn style_raised color_primary icon_none" href="'.$get_dllink.'"><span class="w-btn-label">DOWNLOAD</span><span class="ripple-container"></span></a>
</div>
</div>
<div class="w-separator type_default size_medium thick_1 style_solid color_border cont_none">
<span class="w-separator-h"></span>
</div>';
}
wp_reset_postdata();
return $output;
}
}
add_shortcode('articles_feed', 'articles_download_feed');
How can I create a code which would search by slug(category name) but not by ID. How can I create a customized solution for my client for search within a specific category (without any plugins)?
Explain: I want the code of search to be customized in such a way that the client could search within a specific category without editing any code. Thanks!
is it's still actually, you should to check this tutorial where an author gives a very detailed explanation about the Search function customization.
One of the most interesting parts for a necessary your case:
// meta_query expects nested arrays even if you only have one query
$sm_query = new WP_Query( array( 'post_type' => 'accommodation', 'posts_per_page' => '-1', 'meta_query' => array( array( 'key' => '_sm_accommodation_city' ) ) ) );
// The Loop
if ( $sm_query->have_posts() ) {
$cities = array();
while ( $sm_query->have_posts() ) {
$sm_query->the_post();
$city = get_post_meta( get_the_ID(), '_sm_accommodation_city', true );
// populate an array of all occurrences (non duplicated)
if( !in_array( $city, $cities ) ){
$cities[] = $city;
}
}
}
} else{
echo 'No accommodations yet!';
return;
}
/* Restore original Post Data */
wp_reset_postdata();
if( count($cities) == 0){
return;
}
asort($cities);
$select_city = '<select name="city" style="width: 100%">';
$select_city .= '<option value="" selected="selected">' . __( 'Select city', 'smashing_plugin' ) . '</option>';
foreach ($cities as $city ) {
$select_city .= '<option value="' . $city . '">' . $city . '</option>';
}
$select_city .= '</select>' . "\n";
reset($cities);
$args = array(
'orderby' => 'display_name'
);
$wp_user_query = new WP_User_Query($args);
$authors = $wp_user_query->get_results();
if (!empty($authors))
{
echo '<ul>';
foreach ($authors as $author)
{
$author_info = get_userdata($author->ID);
echo '<li>'.$author->ID.' '.$author_info->first_name.' '.$author_info->last_name.'</li>';
}
echo '</ul>';
} else {
echo 'No authors found';
}
I'm using post filter by post author. Above code displays all the users so need to display only authors who posted in the blog.
just like wp_list_authors() function but i need to get the author id intead of author name. because I need to create a dropdown list. While someone change the option, then I need to get the post by that author in AJAX
Update 1:
Hope this help:
$posted = get_posts([
'post_type' => 'your_custom_post_type',
]);
$author_ids = [];
foreach ($posted as $post) {
$author_ids[] = $post->post_author;
}
$author_ids = array_unique($author_ids);
if (!empty($author_ids) )
{
echo '<ul>';
foreach ($author_ids as $user_id)
{
$author_info = get_userdata($user_id);
echo '<li>'.$user_id.' '.$author_info->first_name.' '.$author_info->last_name.'</li>';
}
echo '</ul>';
} else {
echo 'No authors found';
}
Make sure to change post_type to your post type and each user has both first_name and last_name.
There's a missing argument on Codex reference: has_published_posts.
$args = [
'orderby' => 'display_name',
'has_published_posts' => true
];
$authors = new WP_User_Query($args);
...
It's better to look for inside class file because info on Codex is not always up-to-date.
I'm learning how to create custom plugins for Wordpress, I am trying to get related posts by category.
The problem is, I am returning all posts regardless of it's category whether it's the same category or not.
I've done a var_dump on the $categoriesIds[] and it is pulling the right category for each post.
I'm guessing something is not right with the WP_Query?
Can someone point out what is missing with the code?
function Add_related_posts($content) {
// If it's not a singular post, return the content
if (!is_singular('post')) {
return $content;
}
// Get post categories
$categories = get_the_terms(get_the_ID(), 'category');
$categoriesIds = [];
foreach ($categories as $category) {
$categoriesIds[] = $category->term_id;
}
$loop = new WP_Query(array(
'category_in' => $categoriesIds,
'posts_per_page' => 4,
'post_not_in' => array(get_the_ID()),
'orderby' => 'rand'
));
// If there are posts
if ($loop->have_posts()) {
$content .= 'RELATED POSTS:<br><ul>';
while ($loop->have_posts()) {
$loop->the_post();
$content .= '<li>' . get_the_title() . '</li>';
}
}
$content .= '</ul>';
// Restore data
wp_reset_query();
return $content;
}
Categories are pulled as expected. But in argument in WP_Query, you got one problem.
It should be category__in, NOT category_in.
Try this:
'category__in' => $categoriesIds,
See documentation: http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters