how to enable portfolio in wordpress? - wordpress

is there any way to active portfolio section by adding some code to functions of theme ?
i saw some themes that have this feature , that themes add a new section named portfolio to back-end of Word-press

You can use custom post type to accomplish what you want.
WordPress can hold and display many different types of content. A
single item of such a content is generally called a post, although
post is also a specific post type. Internally, all the post types are
stored in the same place, in the wp_posts database table, but are
differentiated by a column called post_type.
PHP EXAMPLE:
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'portfolio',
array(
'labels' => array(
'name' => __( 'Portfolios' ),
'singular_name' => __( 'Portfolio' )
),
'public' => true,
'has_archive' => true,
)
);
}
Then to add it in your theme you can use WP_Query.
EDIT:
WP_Query Example
$args = array(
'post_type' => 'portfolio'
); // these arguments are telling WP_Query to only look for the post types called portfolio.
$query = new WP_Query( $args );
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
the_post_thumbnail();
<?php endwhile; ?>
<!-- end of the loop -->
Ask me for any confusions.
NOTE: I am showing you a method without using any plugins. A custom approach.

Related

Woocommce: Category Product Page with products grouped by a Custom Taxonomy

I need to show a Category Product Page (Woocommerce)and grouped them by a Custom Taxonomy which is created the Custom Post Type (CPT) plugin.
Here a capture to see what I mean: https://docs.google.com/drawings/d/1JDxGOO2CCq6aGSUwzows7wtAn9DmshCF7M8g-PdcdUc/edit?usp=sharing
Here the Custom Post Type (CPT) plugin: https://es.wordpress.org/plugins/custom-post-type-ui/
Is there a way to do this? I think of a HOOK for the Category page where I can get the Custom Taxonomy value to group the product and finally print them out... not sure.
Should I start with this?
/***********************************************************/
/********** CATEGORY GROUPED BY CPT TAXONOMY ************/
/***********************************************************/
// define the woocommerce_shop_loop callback
function my_woocommerce_shop_loop( $array, $int ) {
Fetch products and Custom Taxonomies values to group them.
};
// add the action
add_action( 'woocommerce_shop_loop', 'my_woocommerce_shop_loop', 10, 2 );
It's not the exact solution to what you are asking but it's an alternate and it may help, so I'm posting it.
Either using a plugin or not, a way to loop through the terms of your taxonomy and display the items of each term, is the below code
<?php
$terms = get_terms([
'taxonomy' => 'YOUR_CUSTOM_TAXONOMY_HERE',
]);
foreach($terms as $term) {
$args = array(
'post_type' => 'YOUR_CUSTOM_POST_TYPE',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'YOUR_CUSTOM_TAXONOMY_HERE',
'field' => 'slug',
'terms' => $term->slug,
)
)
);
$the_new_query = new WP_Query( $args );
if ( $the_new_query->have_posts() ) :
echo '<h1>'.$term->name.'</h1>';
echo '<ul>';
while ( $the_new_query->have_posts() ) : $the_new_query->the_post();
'<li>'.the_title().'</li>';
endwhile;
echo '</ul>';
endif;
}
?>

Filtering Products of same price in one page wordpress

So i have an online store with variations of products but ill like to have a page that shows products with a price range. Say I have a page called 3k Store. I want such that when a user goes to that page, it filters and displays only goods that cost 3000, Please how do i go about this? I am using wordpress and woocommerce. Thanks.
1. Create a page template to list products
Create a page template to be able to put some code on it. Name it page.list-product-in-range.php for example and add this code at the start of the file to make it a template :
<?php // Template Name: List product in range ?>
<?php get_header(); ?>
<?php get_footer(); ?>
2. Create a page in the wordpress admin
Create a page named Product that cost 3000for example and select the template previously created. That's all for the moment.
3. Go to the php file that we created in part 1
Between <?php get_header(); ?> and <?php get_footer(); ?>, add this part to get the products that cost 3000 :
( I let the range in the meta_query so you can easily set something else than 3000 by playing with the number inside value)
<?php
$products = get_posts(
array(
'posts_per_page' => -1,
'post_type' => array('product', 'product_variation'),
'meta_query' => array(
array(
'key' => '_price',
'value' => array(
3000 ,
3000
),
'compare' => 'BETWEEN',
'type'=> 'NUMERIC'
)
)
)
);
?>
4. Add the loop to list products
After the code that we added in part 3 put this code :
<ul class="products">
<?php
if (!empty($products)) {
foreach ( $products as $post ) : setup_postdata($post);
wc_get_template_part( 'content', 'product' );
endforeach;
} else {
echo __( 'No products found', 'woocommerce' );
}
wp_reset_postdata();
?>
</ul>
You also can use pre_get_posts hook to set the products in the main query or instead of get_posts() use the WP_Query class. I only use get_posts() for the simplicity .
If you need pagination (and i think you will need), you should think about using pre_get_posts or WP_Query.
Code tested and it works.

WordPress Adding Variables to Loop Arguments

I have a custom post-type call Called 'Sectors' and another post type called 'Challenges' The challenges post type has a taxonomy called 'sectortype' - which has the same names as the sectors.
I created a page called 'single-sector.php' On that page displays a loop that includes challenges related to that sector.
When I write the loop for displaying challenges, how do I make the 'sectortype' => 'advanced-education' a variable so it will work on other single sector pages?
Here's what I have for the loop...
<?php $challenge_args = array(
'post_type' => 'challenge',
'sectortype' => 'advanced-education', //Need Help Here
);
// create a new instance of WP_Query
$challenge_query = new WP_Query( $challenge_args );
?>
<?php if ( $challenge_query->have_posts() ) : while ($challenge_query->have_posts() ) : $challenge_query->the_post(); // run the loop ?>
Get Custom posts by custom taxonomy terms :
<?php
$terms = get_terms('sectortype');
$challenge_args = array(
'post_type' => 'challenge',
'publish_status' => 'published',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'sectortype',
'field' => 'slug',
'terms' => $terms[0], //whichever term you want to select
),
),
);
// create a new instance of WP_Query
$challenge_query = new WP_Query( $challenge_args );
?>
<?php if ( $challenge_query->have_posts() ) : while ($challenge_query->have_posts() ) : $challenge_query->the_post(); // run the loop ?>
DISPLAY IN SEPARATE PAGES
TO display the posts in separate pages as you mentioned in the comment, you have to do the following:
Create Separate Page Links:: (use on page as navigation items)
<?php $categories = get_terms('sectortype');?>
<ul>
<?php foreach( $categories as $key => $c ):?>
<?php $cat_link = get_term_link( $c->term_id );?>
<?php $term_title= single_term_title('', false);?>
<li class="<?php echo ($c->name == $term_title )?'active':'';?>"><?php echo $c->name;?></li>
<?php endforeach;?>
</ul>
Create a file in theme directory (actually an archive template for taxonomy terms) with the filename 'taxonomy-sectortype.php'.
On that template, get the posts from the usual loop without using any queries and you will get the respective posts.

How to hook in a custom query to the loop with Wordpress

I want to create a loop and query posts by their author role. And display the results of a search term based on the authors role.
I've tried creating a function to alter the query's where clause:
$ids = get_users(
array(
'role' => 'administrator' ,
'fields' => 'ID'
)
);
$query = new WP_Query(
array(
'author__in' => $ids,
)
);
// If the query has data
if($query->have_posts() ) :
// Post loop
while ($query->have_posts() ) :
// Setup post data
$query->the_post();
?>
<!-- Do HTML markup and template tags here, eg. the_content(), the_title() etc.. -->
<h1>You're a post from administrator - <?php the_title(); ?></h1>
<?php get_template_part( 'template-parts/content', 'search' ); ?>
<?php
endwhile;
// End "If the query has data"
endif;
I'm trying to add a WP_Query to the loop but this is where I get stuck with the results not getting filtered by role so I'm fairly certain I must be implementing this wrong - this is the first time I've tried to do something like this so sorry if it's a dump question but I can't find an answer to my question so if anyone can point me in the right direction that would be amazing!
Any advice welcome, thank you!
You can try the posts_where
hook.
UPDATE:
Why don't you use the default WP_Query author arg instead hook.
You query will be :
$ids = get_users(
array(
'role' => 'administrator' ,
'fields' => 'ID'
)
);
$query = new WP_Query(
array(
'author__in' => $ids,
)
);

Custom single-post pulling from wrong custom post type

So I am setting up a custom WordPress theme using two custom post types Articles and News. Each one is also using a custom single-post.php page single-articles.php and single-news.php. But after pulling the article posts on the main page and inserting their permalink into a 'Read more' link when I click on one of the article's links it takes me to their permalink URL but instead of displaying the article post it displays the News post content. Both single-articles.php and single-news.php are in the themes folder, not in a subfolder.
Here is an example of how I'm setting up the custom posts...
add_action( 'init', 'articles_custom_postypes' );
function articles_custom_postypes() {
$args = array (
'label' => $labels,
'public' => true,
'menu_position' => 100,
'menu_icon' => 'dashicons-format-aside',
'label' => 'Articles',
'has_archive' => false,
'supports' => array( 'title','editor', 'thumbnail'),
);
register_post_type( 'articles', $args);
}
On the custom single post pages I am just calling the_title(), the_content, etc. Should I be using a while loop or something? Any advice would be awesome. Thanks!
For this you have to have two files in your theme folder. One is single-articles.php and other is content-articles.php. If the files are created add the following code to single-articles.php.
<?php
while ( have_posts() ) : the_post();
get_template_part( 'content-articles', get_post_format() );
endwhile;
?>
Also add the following to content-articles.php.
<?php
the_title();
the_content();
?>

Resources